package com.zuoqi.easywork.userManage.service;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.easywork.system.service.OpLogService;
import com.zuoqi.easywork.userManage.mapper.TcUserAttendMapper;
import com.zuoqi.easywork.userManage.model.TcUserAttend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AttendService {
    @Autowired
    private TcUserAttendMapper tcUserAttendMapper;
    @Autowired
    private OpLogService opLogService;

    public int recordAttendByIds(IMap param){
        String[] ids = param.getString("ids").split(",");
        String type = param.getString("type");
        TcUserAttend tcUserAttend = new TcUserAttend();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
        String date = tempDate.format(new Date());
        try{
            for(int i = 0;i<ids.length;i++){
                tcUserAttend.setAttendId(date + ids[i]);
                tcUserAttend.setAttendType(type);
                tcUserAttend.setAttendDate(new Date());
                tcUserAttend.setUserId(ids[i]);
                TcUserAttend result =tcUserAttendMapper.selectByPrimaryKey(date + ids[i]);
                if (result != null ){
                    tcUserAttendMapper.updateByPrimaryKeySelective(tcUserAttend);
                }else {
                    tcUserAttendMapper.insert(tcUserAttend);
                }
            }
            opLogService.addLog(param.getString("updateUser"),"出勤记录","记录用户ID："+ids+"状态："+type);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
